/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package verify.ucm;
import java.io.*;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import verify.model.ButtonModel;
import verify.view.DisplayInfo;

/**
 *
 * @author Donghui
 */

public class SetPanelValue {

    private ButtonModel button;
    private String name;
    private String gender;
    private String address;
    private String phone;
    private String group;
    private String id;
    private JLabel jphoto;
    private JTextField jname;
    private JTextField jgender;
    private JTextField jaddress;
    private JTextField jphone;
    private JTextField jgroup;
    private String query;
    private DisplayInfo panel;
    private String path="D:\\TP_BUFFER\\";

    public SetPanelValue(DisplayInfo remote, String sql)
    {

        this.query=sql;
        this.panel=remote;
    }

    public void readimage(){
        button=panel.getButtonModel();
        jphoto=button.getPhoto();
        jname=button.getName();
        jgender=button.getGender();
        jgroup=button.getGroup();
        jname=button.getName();
        jphone=button.getPhone();
        jaddress=button.getAddress();
        final String url="jdbc:mysql://localhost/isc?user=root&password=root&useUnicode=true";
        byte[] buffer=new byte[4096];
        Connection imgcon=null;
        PreparedStatement imgpst=null;
        ResultSet rs=null;
        File file=null;
        InputStream inputimage=null;
        OutputStream outputimage=null;

        try {
            Class.forName("com.mysql.jdbc.Driver");

            imgcon = DriverManager.getConnection(url);
            imgcon.setCatalog("isc");
            
            imgpst=imgcon.prepareStatement(query);
            rs=imgpst.executeQuery();

            rs.next();
                name=rs.getString("name");
                id=rs.getString("id");
                address=rs.getString("address");
                gender=rs.getString("gender");
                phone=rs.getString("tele");
                group=rs.getString("groups");
                path=path+id+".jpg";
            file=new File(path);
            if(!file.exists())
            {
                try{
                 file.createNewFile();
                }
                catch(IOException e)
                {
                e.printStackTrace();
                }
            }
            try{
                outputimage=new FileOutputStream(file);
            }
            catch(FileNotFoundException e)
            {
                e.printStackTrace();
            }

                Blob image=rs.getBlob("photo");
                inputimage=image.getBinaryStream();
                int size;
                try{
                    while((size=inputimage.read(buffer))!=-1)
                    {
                     outputimage.write(buffer, 0, size);
                    }
                }
                    catch (IOException e) {
                    e.printStackTrace();
                    }

                outputimage.close();
                inputimage.close();
                imgpst.close();
                imgcon.close();

        Image imgTemp=Toolkit.getDefaultToolkit().createImage(path);
        jphoto.setIcon(new ImageIcon(imgTemp));
        jname.setText(name);
        jgroup.setText(group);
        jgender.setText(gender);
        jphone.setText(phone);
        jaddress.setText(address);
    }
        catch(Exception e)
        {
            System.out.println(e);
        Image imgTemp=Toolkit.getDefaultToolkit().createImage("D:\\TP_PRUNED\\error.jpg");
        jphoto.setIcon(new ImageIcon(imgTemp));
        jname.setText("N/A");
        jgroup.setText("N/A");
        jgender.setText("N/A");
        jphone.setText("N/A");
        jaddress.setText("N/A");
        }
    }

}
